import { http } from "@/utils/http";
import type { HttpResult } from "@/utils/http/types";

export type UserResult = {
  code: number;
  flag: boolean;
  msg: string;
  data: {
    /** 用户名 */
    username: string;
    /** 当前登陆用户的角色 */
    roles: Array<string>;
    /** `token` */
    accessToken: string;
    /** 用于调用刷新`accessToken`的接口时所需的`token` */
    refreshToken: string;
    /** `accessToken`的过期时间（格式'xxxx/xx/xx xx:xx:xx'） */
    expires: Date;
  };
};

export type RefreshTokenResult = {
  code: number;
  flag: boolean;
  msg: string;
  data: {
    /** `token` */
    accessToken: string;
    /** 用于调用刷新`accessToken`的接口时所需的`token` */
    refreshToken: string;
    /** `accessToken`的过期时间（格式'xxxx/xx/xx xx:xx:xx'） */
    expires: Date;
  };
};

export type RoutesResult = {
  code: number;
  flag: boolean;
  msg: string;
  data: Array<any>;
};

/** 登录 */
export const login = (data?: object) => {
  return http.request<UserResult>("post", "/auth/form", { data });
};

/** 刷新token */
export const refreshToken = (data?: object) => {
  return http.request<RefreshTokenResult>("post", "/user/refreshToken", {
    data
  });
};

export const getAsyncRoutes = () => {
  return http.request<RoutesResult>("get", "/user/asyncRoutes");
};

export function getUserInfo() {
  return http.request<HttpResult>("get", "/user/profile");
}

export function getAuthUser() {
  return http.request<HttpResult>("get", "/user/info");
}

export function logout() {
  return http.request<HttpResult>("post", "/auth/logout");
}

export async function validPasswordRight(password: string) {
  const params = {
    password: password
  };
  return http.request<HttpResult>("get", "/user/password/right", { params });
}

export async function validPasswordExpire() {
  return http.request<HttpResult>("get", "/user/password/expire");
}
